package com.example.rbac.common.config;

import com.example.rbac.common.HttpInterceptor;
import com.example.rbac.common.filter.AclControlFilter;
import com.example.rbac.common.filter.LoginFilter;
import com.google.common.collect.Maps;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.Arrays;
import java.util.Map;

/**
 * @author XieLongzhen
 * @date 2018/12/26 19:19
 */
@Configuration
public class WebAppConfig extends WebMvcConfigurationSupport {

    @Bean
    public MappingJackson2JsonView jsonView() {
        return new MappingJackson2JsonView();
    }

    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");
        registry.addResourceHandler("/bootstrap3.3.5/**").addResourceLocations("classpath:/bootstrap3.3.5/");
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
        registry.addResourceHandler("/ztree/**").addResourceLocations("classpath:/ztree/");
        super.addResourceHandlers(registry);
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HttpInterceptor())
                .addPathPatterns("/**")
                // 不拦截静态资源的请求
                .excludePathPatterns("/css/**", "/js/**", "/assets/**", "/bootstrap3.3.5/**", "/ztree/**");
        super.addInterceptors(registry);
    }

    @Bean
    public FilterRegistrationBean loginFilterRegistration() {
        FilterRegistrationBean<LoginFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new LoginFilter());
        registration.setUrlPatterns(Arrays.asList("/sys/*", "/admin/*"));
        registration.setOrder(9);
        return registration;
    }

    @Bean
    public FilterRegistrationBean aclControlFilterRegistration() {
        FilterRegistrationBean<AclControlFilter> registration = new FilterRegistrationBean<>();

        Map<String, String> initParams = Maps.newHashMap();
        initParams.put("targetFilterLifecycle", "true");
        initParams.put("exclusionUrls", "/sys/user/noAuth.page,/login.page");

        registration.setFilter(new AclControlFilter());
        registration.setUrlPatterns(Arrays.asList("/sys/*", "/admin/*"));
        registration.setInitParameters(initParams);
        registration.setOrder(10);
        return registration;
    }
}
